import wmi
import os
import socket
import GetUser
w = wmi.WMI()
strs = ''
#获取电脑使用者信息
for CS in w.Win32_ComputerSystem():
    print('-------------------------ComputerSystem-------------------------')
    print("电脑名称: %s" %CS.Caption)
    print("使用者: %s" %CS.UserName)
    print("制造商: %s" %CS.Manufacturer)
    print("系统信息: %s" %CS.SystemFamily)
    print("工作组: %s" %CS.Workgroup)
    print("机器型号: %s" %CS.model)
    strs = strs + f'''电脑名称: {CS.Caption}
使用者: {CS.UserName}
制造商: {CS.Manufacturer} 
系统信息: {CS.SystemFamily} 
工作组: {CS.Workgroup} 
机器型号: {CS.model}''' 
#获取操作系统信息
for OS in w.Win32_OperatingSystem():
    print('-------------------------OperatingSystem-------------------------')
    #print(OS)
    print("操作系统: %s" %OS.Caption)
    print("语言版本: %s" %OS.MUILanguages)
    print("系统位数: %s" %OS.OSArchitecture)
    print("注册人: %s" %OS.RegisteredUser)
    print("系统驱动: %s" %OS.SystemDevice)
    print("系统目录: %s" %OS.SystemDirectory)
    strs = strs + f'''
操作系统: {OS.Caption} 
语言版本: {OS.MUILanguages} 
系统位数: {OS.OSArchitecture} 
注册人: {OS.RegisteredUser} 
系统驱动: {OS.SystemDevice} 
系统目录: {OS.SystemDirectory} '''
#获取电脑IP和MAC信息
for address in w.Win32_NetworkAdapterConfiguration(ServiceName = "e1dexpress"):
    print('-------------------NetworkAdapterConfiguration-------------------')
    #print(address)
    print("IP地址: %s" % address.IPAddress)
    print("MAC地址: %s" % address.MACAddress)
    print("网络描述: %s" % address.Description)
    strs = strs + f'''
IP地址: {address.IPAddress} 
MAC地址: {address.MACAddress}
网络描述: {address.Description}'''
#获取电脑CPU信息
for processor in w.Win32_Processor():
    print("----------------------------Processor----------------------------")
    #print(processor)
    print("CPU型号: %s" % processor.Name.strip())
    print("CPU核数: %s" % processor.NumberOfCores)
    strs = strs + f'''
CPU型号: {processor.Name.strip()} 
CPU核数: {processor.NumberOfCores} '''
#获取BIOS信息
for BIOS in w.Win32_BIOS():
    print("-------------------------------BIOS-------------------------------")
    #print(BIOS)
    print("使用日期: %s" %BIOS.Description)
    print("主板型号: %s" %BIOS.SerialNumber)
    print("当前语言: %s" %BIOS.CurrentLanguage)
    strs = strs + f'''
使用日期: {BIOS.Description}
主板型号: {BIOS.SerialNumber}
当前语言: {BIOS.CurrentLanguage}'''
#获取内存信息
for memModule in w.Win32_PhysicalMemory():
    print("--------------------------PhysicalMemory--------------------------")
    totalMemSize = int(memModule.Capacity)
    print("内存厂商: %s" %memModule.Manufacturer)
    print("内存型号: %s" %memModule.PartNumber)
    print("内存大小: %.2fGB" %(totalMemSize/1024**3))
    strs = strs + f'''
内存厂商: {memModule.Manufacturer})
内存型号: {memModule.PartNumber})
内存大小: %.2fGB"'''%(totalMemSize/1024**3)
#获取磁盘信息
for disk in w.Win32_DiskDrive():
    print("-----------------------------DiskDrive----------------------------")
    diskSize = int(disk.size)
    print("磁盘名称: %s" %disk.Caption)
    print("硬盘型号: %s" %disk.Model)
    print("磁盘大小: %.2fGB" %(diskSize/1024**3))
    strs = strs + f'''
磁盘名称: {disk.Caption})
硬盘型号: {disk.Model})
磁盘大小: %.2fGB'''%(diskSize/1024**3)
#获取显卡信息
for xk in w.Win32_VideoController():
    print('--------------------------VideoController-------------------------')
    print("显卡名称: %s" %xk.name)
    strs = strs + f'''
显卡名称: {xk.name}'''
#获取计算机名称和IP
print("---------------------------ComputerName---------------------------")
hostname = socket.gethostname()
ip = socket.gethostbyname(hostname)
print("计算机名称: %s" %hostname)
print("IP地址: %s" %ip)
strs = strs + f'''
计算机名称: {hostname}
IP地址: {ip}'''
strs = strs + f'''
所有用户名: {str(GetUser.main())}'''
with open('ComputerConfig.cfg','w') as f:
    f.write(strs)
